using System.Data;
using System.Linq.Expressions;
using SqlSugar;

namespace Gateway.IRepositories;

public interface IQueryRepository
{
    public SqlSugarScope Db { get; }

    /// <summary>
    /// 查询一条记录
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sql"></param>
    /// <param name="param">查询参数</param>
    /// <returns></returns>
    T QueryFirstOrDefault<T>(string sql, object param = null);

    /// <summary>
    /// 查询一条记录 异步
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sql"></param>
    /// <param name="param">查询参数</param>
    /// <returns></returns>
    Task<T> QueryFirstOrDefaultAsync<T>(string sql, object param = null);

    /// <summary>
    /// 查询一条记录 动态类型 异步
    /// </summary>
    Task<dynamic> QueryFirstOrDefaultAsync(string sql, object param = null);

    /// <summary>
    /// 查出多条记录的实体泛型集合
    /// </summary>
    /// <typeparam name="T">泛型T</typeparam>
    /// <returns></returns>
    IEnumerable<T> Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true,
        int? commandTimeout = null, CommandType? commandType = null);

    /// <summary>
    /// 查出多条记录的实体泛型集合 异步
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sql">sql语句</param>
    /// <param name="param">查询参数</param>
    /// <param name="transaction">所使用的事务</param>
    /// <param name="commandTimeout">执行超时时间</param>
    /// <param name="commandType">语句类型</param>
    /// <returns></returns>
    Task<IEnumerable<T>> QueryAsync<T>(string sql, object param = null, IDbTransaction transaction = null,
        int? commandTimeout = null, CommandType? commandType = null);

    /// <summary>
    /// 查出多条记录的实体泛型集合 异步
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sql">sql语句</param>
    /// <param name="param">查询参数</param>
    /// <param name="transaction">所使用的事务</param>
    /// <param name="commandTimeout">执行超时时间</param>
    /// <param name="commandType">语句类型</param>
    /// <returns></returns>
    Task<List<T>> QueryListAsync<T>(string sql, object param = null, IDbTransaction transaction = null,
        int? commandTimeout = null, CommandType? commandType = null);


    /// <summary>
    /// 查出多条记录的实体泛型集合 动态类型 异步
    /// </summary>
    /// <param name="sql">sql语句</param>
    /// <param name="param">查询参数</param>
    /// <param name="transaction">所使用的事务</param>
    /// <param name="commandTimeout">执行超时时间</param>
    /// <param name="commandType">语句类型</param>
    Task<IEnumerable<dynamic>> QueryAsync(string sql, object param = null, IDbTransaction transaction = null,
        int? commandTimeout = null, CommandType? commandType = null);

    /// <summary>
    /// 查找单个数据
    /// </summary>
    /// <param name="expression"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    Task<T> FindAsync<T>(Expression<Func<T, bool>> expression);
   
    /// <summary>
    /// 查找单个数据- 分表
    /// </summary>
    /// <param name="splitId"></param>
    /// <param name="expression"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    Task<T> FindSplitAsync<T>(object splitId,Expression<Func<T, bool>> expression);

    /// <summary>
    /// 查找单个数据- 分表
    /// </summary>
    /// <param name="splitId"></param>
    /// <param name="expression"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    Task<T> FindSplitAsync<T>( Expression<Func<T, bool>> expression);
    /// <summary>
    /// 查找列表数据
    /// </summary>
    /// <param name="expression"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    Task<List<T>> GetListAsync<T>(Expression<Func<T, bool>> expression);
}